<template>
    <div>
        <el-alert v-if="tis" :title="tis" type="success" show-icon :closable="false"/>
       <el-table :data="this.tableData" ref="table" :row-key="getRowKeys" @selection-change="handleSelectionChange" stripe>
        <tr v-for="(item, index) in this.tableLabel" :key="index">
            <el-table-column v-if="item.type==='checkbox'" :fixed="item.type==='checkbox'?'left':'none'" type="selection" width="55" :reserve-selection="true"></el-table-column>
            <el-table-column v-else-if="item.type==='createTime'" :prop="item.prop" :label="item.label" :min-width="item.width" :formatter="formatDate">
            </el-table-column>
            <el-table-column v-else-if="item.type==='updateTime'" :prop="item.prop" :label="item.label" :min-width="item.width" :formatter="formatDate">
            </el-table-column>
            <el-table-column v-else-if="item.type==='normal'" :prop="item.prop" :label="item.label" :width="item.width"></el-table-column>
        </tr>
          <el-table-column label="操作" :width="this.models.width" :fixed="this.models.fixed">
              <template slot-scope="scope">
                  <el-button type="warning" v-if="scope.row.edit" size="mini" @click="edit(scope.row)">修改</el-button>
                  <el-button type="danger" v-if="scope.row.del" size="mini" @click="del(scope.row)">删除</el-button>
                  <el-button type="primary" v-if="scope.row.details" size="mini" @click="details(scope.row)">详情</el-button>
              </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-pagination
            class="page-footer"
            background
            layout="total,prev,pager,next,sizes,jumper"
            :total="this.models.total"
            :page-sizes="[10,20,30,40,50]"
            @current-change="handlePageChange"
            @size-change="handleSizeChange"
          />
        </el-row>
    </div>
  </template>
  
  <script>
      import moment from 'moment'
  export default {
      props: {
          tableData: {
            type: Array,
            default: () => {
                return [];
            }
          },
          tableLabel:{
            type: Array,
            default: () => {
                return [];
            }
          },
          models: {
            type: Object,
            default: () => {
                return null;
            }
          },
      },
      data() {
          return {
            tis:"",
            ids:[],
               params: {
                  type:"page",
                  pageNum: 1,
                  pageSize: 10
              },
          };
      },
      mounted(){
        if(this.models.hackReset){
          this.$refs.table.clearSelection();
          this.ids = [];
          this.tis = "";
        }
      },
      methods: {
        formatDate(row, column) {
            const date = row[column.property]
            if (date == undefined) {
            return ''
            }
            return moment(date).format("YYYY-MM-DD  HH:mm:ss")
        },
        getRowKeys(row) {
            return row.id;
        },
        handleSelectionChange(val) {
          let ids = [];
          val.forEach(item =>{ 
              ids.push(item)
          });
          this.ids = ids;
          this.tis="已选择 "+val.length+" 项";
          if(val.length==0){
            this.tis="";
            this.ids = [];
          }
          this.$emit("on-selection-click", this.ids);
        },
          edit(row){
              this.$emit("on-edit-click",row);
          },
          del(row){
              this.$emit("on-del-click",row);
          },
          details(row){
              this.$emit("on-details-click",row);
          },
          handlePageChange(page) {
             this.params.pageNum = page;
              this.loadList();
          },
          handleSizeChange(page) {
              this.params.pageSize = page;
              this.loadList();
          },
          loadList() {
              this.$emit("on-search-click", this.params);
          },
          onClose() {
              this.$emit("on-cancel");
          }
      }
  };
  </script>
  
  